package com.example.demo.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.po.User;
import com.example.demo.service.UserService;

@RestController
@RequestMapping("/api/user")
public class UserApi {
	
	@Autowired
	private UserService UserService;

	@RequestMapping("/all")
	public List<User> all() {
		return UserService.selectAll();
	}

	@RequestMapping("/one")
	public User one(Integer id) {

		return UserService.selectById(id);
	}

	@RequestMapping("/one1/{id}")
	public User one1(@PathVariable("id") Integer id) {
		return UserService.selectById(id);
	}

	@RequestMapping("/add")
	public String add(User User) {
		UserService.insert(User);
		return "添加成功！";

	}

	@RequestMapping("/add1")
	public String add(@RequestBody JSONObject json) {
		User User = null;
		User = JSON.toJavaObject(json, User.class);
		System.out.println("添加成功");
		UserService.insert(User);
		return "添加成功";

	}

	@RequestMapping("/update1")
	public String update(@RequestBody JSONObject json) {
		System.out.println(11111111);
		User User = null;

		User = JSON.toJavaObject(json, User.class);
		UserService.update(User);
		return "修改成功";
	}

	@RequestMapping("/update")
	public String update(User User) {
		UserService.update(User);
		return "修改成功";
	}

	@RequestMapping("/delete")
	public String delete(Integer id) {
		UserService.delete(id);
		return "删除成功！";
	}

	@RequestMapping("/delete1/{id}")
	public String delete1(@PathVariable("id") Integer id) {
		UserService.delete(id);
		return "删除成功";
	}

}
